export default function def(obj, key, val, enumerable) {
  Object.defineProperty(obj, key, {
    value: val,
    enumerable: !!enumerable,
    writable: true,
    configurable: true,
  });
}

let strats = {};
const LIFECYCLE_HOOKS = [
  "beforeCreate",
  "created",
  "beforeMount",
  "mounted",
  "beforeUpdate",
  "updated",
  "beforeDestroy",
  "destroyed",
];
LIFECYCLE_HOOKS.forEach((hook) => {
  strats[hook] = mergeHook;
});

export { strats };

export function mergeHook(parentVal, childVal) {
  if (childVal) {
    if (parentVal) {
      return parentVal.concat(childVal); //如果儿子存在父亲也存在，那么合并成一个数组
    } else {
      return [childVal]; //如果儿子存在父亲不存在，那么返回一个数组
    }
  } else {
    return parentVal; //如果儿子不存在，那么返回父亲
  }
}

export function mergeOptions(parent, child) {
  const options = {};
  for (let key in parent) {
    mergeField(key);
  }
  for (let key in child) {
    if (!parent.hasOwnProperty(key)) {
      mergeField(key);
    }
  }

  function mergeField(key) {
    if (strats[key]) {
      return (options[key] = strats[key](parent[key], child[key]));
    } else {
      options[key] = child[key] || parent[key];
    }
  }
  return options;
}
